package com.clinic.dao.imp;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.clinic.dao.interfaces.Profile_dao;
import com.clinic.domain.Profile;
import com.clinic.util.Util_imp;

public class Profile_imp implements Profile_dao{

	SessionFactory session_factory = null;

	public Profile_imp() {
		session_factory = Util_imp.get_sessionfactory();
	}

	@Override
	public int create_profile(Profile p) {
		System.out.println("Enter - create_profile");
		Session session = session_factory.openSession();
		Transaction tx = null;
		int pe_obj_id = 0;
		try {
			tx = session.beginTransaction();
			pe_obj_id = (Integer) session.save(p);
			tx.commit();
			System.out.println("New Profile Created with ID: " + pe_obj_id);
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Create Profile Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - create_profile");
		return pe_obj_id;
	}

	@Override
	public void update_profile(Profile pe) {
		System.out.println("Enter - update_profile");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(pe);
			tx.commit();
			System.out.println("Profile Object Updated");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Profile Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - update_profile");
	}

	@Override
	public void delete_profile(int pe_obj_id) {
		System.out.println("Enter - delete_profile");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Profile p = (Profile) session.get(Profile.class, pe_obj_id);
			session.delete(p);
			tx.commit();
			System.out.println("Profile with ID: " + pe_obj_id + " Deleted");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Profile Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - delete_profile");
	}

	@Override
	public Profile get_profile(int pe_obj_id) {
		System.out.println("Enter - get_profile");
		Session session = session_factory.openSession();
		Transaction tx = null;
		Profile p = null;
		try {
			tx = session.beginTransaction();
			p = (Profile) session.createQuery("from Profile where pe_obj_id=:id")
					.setParameter("id", pe_obj_id).uniqueResult();
			tx.commit();
			System.out.println("Returned Object Owner Name: " + p.getObj_owner_name());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_profile");
		return p;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Profile> get_all_profiles() {
		System.out.println("Enter - get_all_profiles");
		Session session = session_factory.openSession();
		List<Profile> l = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			l = session.createQuery("from Profile").list();
			tx.commit();
			System.out.println("Number of Profile returned is: " + l.size());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_all_profiles");
		return l;
	}

	@Override
	public Profile persistOrMerge(Profile p) {
		return (Profile) session_factory.getCurrentSession().merge(p);
	}
	
	@Override
	public int getsize(){
		System.out.println("Enter - getsize");
		Session session = session_factory.openSession();
		int size = 0;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			size = ((Long) session.createQuery("select count(*) from Profile").uniqueResult()).intValue();
			tx.commit();
			System.out.println("Count(*) Profile: " + size);
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - getsize");
		return size;
	}
}